package com.adcc.automation.service.util;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import com.adcc.automation.config.YmlConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XmlConverter {
    private static final Logger logger = LoggerFactory.getLogger(XmlConverter.class);
    public static String convertAcarsToAck(String xmlStr) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));

            // 检查是否存在 ACARS 标签
            Element acarsElement = doc.getDocumentElement();
            if ("ACARS".equals(acarsElement.getTagName())) {
                // 获取 UUID
                Element bodyElement = (Element) acarsElement.getElementsByTagName("Body").item(0);
                String uuid = bodyElement.getAttribute("UUID");

                // 创建 ACK XML
                Document ackDoc = builder.newDocument();
                Element ackRoot = ackDoc.createElement("ACK");
                ackRoot.setAttribute("V", "3.0");
                ackDoc.appendChild(ackRoot);

                Element infoElement = ackDoc.createElement("Info");
                ackRoot.appendChild(infoElement);

                Element typeElement = ackDoc.createElement("Type");
                typeElement.setTextContent("CMD");
                infoElement.appendChild(typeElement);

                Element msgIdElement = ackDoc.createElement("MsgID");
                msgIdElement.setTextContent(uuid);
                infoElement.appendChild(msgIdElement);

                Element recvTimeElement = ackDoc.createElement("RecvTime");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String recvTime = sdf.format(new Date());
                recvTimeElement.setTextContent(recvTime);
                infoElement.appendChild(recvTimeElement);

                // 将 ACK XML 转换为字符串
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

                StringWriter writer = new StringWriter();
                transformer.transform(new DOMSource(ackDoc), new StreamResult(writer));
                return writer.toString();
            }
        } catch (Exception e) {
           logger.error("convertAcarsToAck error", e);
        }
        return null;
    }
    public static boolean isAck(String xmlStr) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
            // 检查是否存在 ACK 标签
            Element acarsElement = doc.getDocumentElement();
            if ("ACK".equals(acarsElement.getTagName())) {
                return true;
            }
        } catch (Exception e) {
           logger.error("isAck error", e);
        }
        return false;
    }
    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ACARS V=\"3.0\"><Body SendTime=\"2025-03-28 05:09:00\" SendID=\"Server\" ReceiveID=\"Terminal\" MsgID=\"3001\" UUID=\"ddd\"></Body><Receiver><nd FlightID=\"CCA1008\" Departure=\"ZBAA\" Destination=\"ZSSS\">30011</nd></Receiver></ACARS>";
        String ackXml = convertAcarsToAck(xml);
        if (ackXml != null) {
            System.out.println(ackXml);
        }
    }
}    